package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description:
 * Author Xu Chen xu
 * Create 2024/4/20 - 上午8:47
 * version v1.0
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/acl/role")
@CrossOrigin
public class RoleController {

    @Resource
    private RoleService roleService;

    //分页查询角色列表
    @ApiOperation("获取角色列表信息接口")
    @GetMapping("{page}/{limit}")
    public Result pageList(
            @PathVariable Long page,
            @PathVariable Long limit,
            RoleQueryVo roleQueryVo) {
        Page<Role> pageParam = new Page<>(page, limit);
        IPage<Role> pageModel = roleService.selectPage(pageParam, roleQueryVo);

        return Result.ok(pageModel);
    }

    @ApiOperation("添加角色接口")
    @PostMapping("save")
    public Result save(@RequestBody Role role) {
        boolean save = roleService.save(role);
        if (save) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    @ApiOperation("按角色Id查询角色接口")
    @GetMapping("get")
    public Result getById(@RequestBody Long id) {
        Role byId = roleService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public Result updateById(@RequestBody Role role) {
        roleService.updateById(role);
        return Result.ok(role);
    }

    @ApiOperation("删除角色接口")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean remove = roleService.removeById(id);
        if (remove) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }

    @ApiOperation("批量删除角色接口")
    @DeleteMapping("batchRemove")
    public Result removeRoles(@RequestBody List<Long> idList) {
        boolean batchRemove = roleService.removeByIds(idList);
        if (batchRemove) {
            return Result.ok(null);
        } else {
            return Result.fail(null);
        }
    }
}
